package net.tygzs.minerpg;

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.logging.Level;
import java.util.logging.Logger;
import net.tygzs.minerpg.CommandExecutor.MineFightingStrength.MineFightingStrength;
import net.tygzs.minerpg.Configuration.PluginConfig;
import net.tygzs.minerpg.Listener.MineRPGListener;
import net.tygzs.minerpg.CommandExecutor.MineItem.MineItem;
import net.tygzs.minerpg.CommandExecutor.MinePacket.MinePacket;
import net.tygzs.minerpg.CommandExecutor.MineRPG.MineRPG;
import net.tygzs.minerpg.CommandExecutor.MineTeam.MineTeam;
import net.tygzs.minerpg.Configuration.pvp.PVPConfig;
import net.tygzs.minerpg.Configuration.TeamConfig;
import net.tygzs.minerpg.keyBoardListener.keyBoardListener;
import net.tygzs.minerpg.lib.FileLib;
import net.tygzs.minerpg.lib.MD5;
import org.bukkit.Bukkit;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.plugin.java.JavaPlugin;

/**
 *
 * @author thtTNT
 */
public class Main extends JavaPlugin
{

    private static Main plugin;
    private static PluginConfig pluginConfig;

    @Override
    public void onEnable()
    {
        CheckUpdata();
        long time = System.currentTimeMillis();
        Main.plugin = this;
        Main.pluginConfig = new PluginConfig();
        getCommand("MineItem").setExecutor(new MineItem());
        getCommand("MinePacket").setExecutor(new MinePacket());
        getCommand("MineFightingStrength").setExecutor(new MineFightingStrength());
        getCommand("MineTeam").setExecutor(new MineTeam());
        getCommand("MineRPG").setExecutor(new MineRPG());
        getServer().getPluginManager().registerEvents(new MineRPGListener(), plugin);
        getLogger().info("RPG插件装载完成,用时" + (System.currentTimeMillis() - time) + "毫秒");
    }

    @Override
    public void onDisable()
    {
        try
        {
            getPluginConfig().save();
            keyBoardListener.server.close();
        } catch (IOException ex)
        {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public static Main getPlugin()
    {
        return Main.plugin;
    }

    public static PluginConfig getPluginConfig()
    {
        return Main.pluginConfig;
    }

    private void CheckUpdata()
    {
        if (!getConfig().getBoolean("AutoUpdata"))
        {
            return;
        }
        UpdataThread ut = new UpdataThread();
        ut.start();
    }

    public class UpdataThread extends Thread
    {

        @Override
        public void run()
        {
            try
            {
                File UpdataFile = new File(getDataFolder(), "Updata");
                if (!UpdataFile.exists())
                {
                    UpdataFile.mkdir();
                }
                URL url = new URL("http://7xpy1t.com1.z0.glb.clouddn.com/least.txt");
                HttpURLConnection HttpUrl = null;
                try
                {
                    HttpUrl = (HttpURLConnection) url.openConnection();
                } catch (IOException ex)
                {
                    getLogger().info("连接更新服务器失败,请前往mcbbs进行手动更新");
                    Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
                }
                HttpUrl.connect();
                try (BufferedInputStream bis = new BufferedInputStream(HttpUrl.getInputStream()); FileOutputStream fos = new FileOutputStream(new File(getDataFolder(), "least.yml")))
                {
                    byte[] buf = new byte[8096];
                    int size;
                    while ((size = bis.read(buf)) != -1)
                    {
                        fos.write(buf, 0, size);
                    }
                }
                FileConfiguration least = YamlConfiguration.loadConfiguration(new File(getDataFolder(), "least.yml"));
                String md5 = MD5.getMd5ByFile(Main.getPlugin().getFile());
                if (!md5.equals(least.getString("MD5")))
                {
                    url = new URL(least.getString("URL"));
                    HttpUrl = (HttpURLConnection) url.openConnection();
                    HttpUrl.connect();
                    BufferedInputStream bis = new BufferedInputStream(HttpUrl.getInputStream());
                    int available = HttpUrl.getContentLength();
                    int size = 0;
                    byte[] buf = new byte[8096];
                    File file = new File(getDataFolder(), "/Updata/" + Main.getPlugin().getFile().getName());
                    FileOutputStream fos = new FileOutputStream(file);
                    while ((size = bis.read(buf)) != -1)
                    {
                        fos.write(buf, 0, size);
                        getLogger().info(file.length() + "Byte/" + available + "Byte");
                    }
                    bis.close();
                    fos.close();
                    if (!getServer().getUpdateFolderFile().exists())
                    {
                        getServer().getUpdateFolderFile().mkdirs();
                    }
                    File PluginFile = new File(getServer().getUpdateFolderFile(), Main.getPlugin().getFile().getName());
                    FileLib.copyFile(file, PluginFile);
                    getLogger().info("更新完成!");
                }
            } catch (MalformedURLException ex)
            {
                Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
            } catch (IOException ex)
            {
                Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

    public static PVPConfig getPVPConfig()
    {
        return Main.getPluginConfig().getPVPConfig();
    }

    public static TeamConfig getTeamConfig()
    {
        return Main.getPluginConfig().getTeamConfig();
    }
}
